-module(client_redis).
-author("ShenDatian").

-behaviour(gen_server).

%% API
-export([start_link/0]).

%% gen_server callbacks
-export([init/1,
  handle_call/3,
  handle_cast/2,
  handle_info/2,
  terminate/2,
  code_change/3]).

-define(SERVER, ?MODULE).

-record(state, {num}).


start_link() ->
  gen_server:start_link({local, ?SERVER}, ?MODULE, [], []).


init([]) ->
  erlang:send_after(1000, self(), {loop}),
  {ok, #state{num=0}}.


handle_call(_Request, _From, State) ->
  {reply, ok, State}.

handle_cast(_Request, State) ->
  {noreply, State}.

handle_info({loop}, #state{num=N}=State) ->
  erlang:send_after(1000, self(), {loop}),
  case N < 10000 of
    true ->
      lists:foreach(fun(I) -> client_redis_u:start(integer_to_list(I)) end, lists:seq(N+1,N+100));
    false ->
      ok
  end,
  {noreply, State#state{num=N+100}};
handle_info(_Info, State) ->
  {noreply, State}.

terminate(_Reason, _State) ->
  ok.

code_change(_OldVsn, State, _Extra) ->
  {ok, State}.

%%%===================================================================
%%% Internal functions
%%%===================================================================


